package tron.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import tron.entity.User;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;

@RestController
@Slf4j
@Api(tags = {"测试"})
public class TronController {

    @ApiOperation(value = "时间测试")
    @GetMapping("/timeTest")
    public Object timeTest(User user) {
        return user;
    }


    /**
     * 时间参数处理参见 {@link tron.config.DateConverter}{@link tron.config.LocalDateTimeConverter}{@link tron.config.TronWebConfig}
     * @return
     */
    @ApiOperation(value = "时间参数测试")
    @GetMapping("/timeParameterTest")
    public Object timeParameterTest() {
        final RestTemplate restTemplate = new RestTemplate();
        final HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("createTime",new Date());
        hashMap.put("birthday", LocalDateTime.now());
        final String forObject = restTemplate.getForObject("http://localhost:8080/timeTest?createTime={createTime}&birthday={birthday}", String.class,hashMap);
        return forObject;
    }

    /**
     * @ApiParam(name = "定义传参的参数名，默认是参数写法",value = "参数描述")
     */
    @ApiOperation(value = "请求参数",notes = "接口描述")
    @GetMapping("/requestParameter")
    public Object requestParameter(@RequestParam @ApiParam(value = "参数描述") String id) {
        return id;
    }

    @GetMapping("/test")
    public Object getCookie(@CookieValue("myCookie") String mycookie ) {
        return mycookie;
    }

    @GetMapping("/getFile")
    public void getFile(HttpServletResponse response) throws IOException {
        String zipName = "myfile.zip";
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition","attachment; filename="+zipName);
        final ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write("this is a file".getBytes());
    }

}
